<template>
    <div>
        <el-row style="margin: 18px 0px 0px 18px ">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">管理中心</el-breadcrumb-item>
                <el-breadcrumb-item>内容管理</el-breadcrumb-item>
                <el-breadcrumb-item>文章管理</el-breadcrumb-item>
            </el-breadcrumb>
        </el-row>
        <el-link href="/admin/content/editor" :underline="false" target="_blank" class="add-link">
            <el-button type="success">写文章</el-button>
        </el-link>
        <el-card style="margin: 18px 2%;width: 95%">
            <el-table
                    :data="articles"
                    stripe
                    style="width: 100%"
                    :max-height="tableHeight">
                <el-table-column
                        type="selection"
                        width="55">
                </el-table-column>

                <el-table-column
                        prop="articleTitle"
                        label="题目（展开查看摘要）"
                        fit>
                </el-table-column>
                <el-table-column type="expand">
                    <template v-slot="props">
                        <el-form label-position="left" inline>
                            <el-form-item>
                                <!--        props.row.articleAbstract          -->
                                <span>{{ props.row.articleAbstract }}</span>
                            </el-form-item>
                        </el-form>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="articleDate"
                        label="发布日期"
                        width="200">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="180">
                    <template v-slot="scope">
                        <!--         .native     -->
                        <el-button
                                @click.prevent="viewArticle(scope.row.id)"
                                type="text"
                                size="small">
                            查看
                        </el-button>
                        <!--         .native     -->
                        <el-button
                                @click.prevent="editArticle(scope.row)"
                                type="text"
                                size="small">
                            编辑
                        </el-button>
                        <!--           .native   -->
                        <el-button
                                @click.prevent="deleteArticle(scope.row.id)"
                                type="text"
                                size="small">
                            移除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="margin: 20px 0 50px 0">
                <el-pagination
                        background
                        style="float:right;"
                        layout="total, prev, pager, next, jumper"
                        @current-change="handleCurrentChange"
                        :page-size="pageSize"
                        :total="total">
                </el-pagination>
            </div>
        </el-card>
    </div>
</template>

<script>
    import {computed, getCurrentInstance, onMounted, reactive, toRefs} from "vue";

    export default {
        name: 'ArticleManagement',
        setup() {
            const data = reactive({
                articles: [],
                pageSize: 10,
                total: 0
            })
            onMounted(() => {
                loadArticles()
            })
            let tableHeight = computed(() => {
                return window.innerHeight - 320
            })

            let {proxy} = getCurrentInstance()

            function loadArticles() {
                proxy.$axios.get('/article/' + data.pageSize + '/1').then(resp => {
                    if (resp && resp.data.code === 200) {
                        data.articles = resp.data.result.content
                        data.total = resp.data.result.totalElements
                    }
                })
            }

            function handleCurrentChange(page) {
                proxy.$axios.get('/article/' + data.pageSize + '/' + page).then(resp => {
                    if (resp && resp.data.code === 200) {
                        data.articles = resp.data.result.content
                        data.total = resp.data.result.totalElements
                    }
                })
            }

            function viewArticle(id) {
                let articleUrl = proxy.$router.resolve(
                    {
                        path: '../../jotter/article',
                        query: {
                            id: id
                        }
                    }
                )
                window.open(articleUrl.href, '_blank')
            }

            function editArticle(article) {
                proxy.$router.push(
                    {
                        name: 'Editor',
                        params: {
                            article: article
                        }
                    }
                )
            }

            function deleteArticle(id) {
                proxy.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                        proxy.$axios
                            .delete('/admin/content/article/' + id).then(resp => {
                            if (resp && resp.data.code === 200) {
                                loadArticles()
                            }
                        })
                    }
                ).catch(() => {
                    proxy.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
            }

            return {
                ...toRefs(data),
                loadArticles,
                onMounted,
                tableHeight,
                editArticle,
                handleCurrentChange,
                deleteArticle,
                viewArticle
            }
        }
    }
</script>

<style scoped>
    .add-link {
        margin: 18px 0 15px 10px;
        float: left;
    }
</style>
